package com.zl.service.impl;

import com.zl.dao.RoleDao;
import com.zl.dao.UserDao;
import com.zl.domain.Privilege;
import com.zl.domain.User;
import com.zl.service.UserService;
import com.zl.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements
        UserService {
    @Autowired
    public UserDao userDao;

    @Autowired
    public RoleDao roleDao;

    @Override
    public Map<String, Object> login(User user) {
        user.setPassword(Util.md5(user.getPassword()));
        return userDao.login(user);
    }

    @Override
    public int insert(User user) {
        user.setUsername(Util.uuid());
        user.setPassword(Util.md5("12345678"));
        user.setRegisterdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date()).toString());
        user.setRid(4);
        return userDao.insert(user);
    }

    @Override
    public int update(User user) {
        if (user.getPassword() != null && !("").equals(user.getPassword()))
            user.setPassword(Util.md5(user.getPassword()));
        return userDao.update(user);
    }

    @Override
    public List<Privilege> findPrivileges(String rid) {
        return userDao.findPrivileges(rid);
    }

    @Override
    public int delUser(String username) {
        return userDao.delUser(username);
    }

    @Override
    public Map<String, Object> findUser(User user) {
        String password = user.getPassword();
        if (!("").equals(password) && password != null)
            user.setPassword(Util.md5(password));
        return userDao.findUser(user);
    }
}
